<!-- 购物车组件 -->
<template>
  <div class="cartcontrol">
    <transition name="move">
      <div class="cart-decrease" v-show="food.count > 0" @click.stop.prevent="decreaseCart($event)">
        <i class="inner icon-remove_circle_outline"></i>
      </div>
    </transition>
    <div class="cart-count" v-show="food.count > 0">{{food.count}}</div>
    <div class="cart-add icon-add_circle" @click.stop.prevent="addCart($event)"></div>
  </div>
</template>

<script>
  import Vue from 'vue';

  export default{
    props: ['food'],
    methods: {
      addCart(event) {
        if (!event._constructed) return;
        !this.food.count ? Vue.set(this.food, 'count', 1) : ++this.food.count;
        this.$parent.$emit('cart.add', event.target);
        this.$parent.$parent.$emit('cart.add', event.target);
      },
      decreaseCart(event) {
        if (!event._constructed) return;
        this.food.count > 0 && this.food.count--;
      }
    }
  }
</script>

<style lang="scss">
  .cartcontrol {
    font-size: 0;
    .cart-decrease {
      display: inline-block;
      padding: 6px;

      &.move-enter-active, &.move-leave-active {
        transition: all .3s linear;
        transform: translate3D(0, 0, 0);
        .inner {
          transition: all .3s linear;
          transform: rotate(0deg);
        }
      }
      &.move-enter, &.move-leave-active {
        transform: translate3D(24px, 0, 0);
        opacity: 0;
        .inner {
          transition: all .3s linear;
          transform: rotate(100deg);
        }
      }

      .inner {
        display: inline-block;
        line-height: 24px;
        font-size: 24px;
        color: rgb(17, 160, 220);
      }
    }
    .cart-count {
      display: inline-block;
      vertical-align: top;
      width: 12px;
      padding-top: 6px;
      line-height: 24px;
      text-align: center;
      font-size: 10px;
      color: rgb(147, 153, 159);
    }
    .cart-add {
      display: inline-block;
      padding: 6px;
      line-height: 24px;
      font-size: 24px;
      color: rgb(17, 160, 220);
    }
  }
</style>
